package cn.kgc.javaweb.service;

import cn.hutool.core.util.ObjectUtil;
import cn.kgc.javaweb.dto.PageDTO;
import cn.kgc.javaweb.vo.SysUserVO;
import cn.kgc.javaweb.entity.SysUser;
import cn.kgc.javaweb.mapper.UserMapper;
import cn.kgc.javaweb.utils.LayUtil;
import cn.kgc.javaweb.utils.MapperUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: mosin
 * @version: v1.0  2022/6/27
 */
public class UserServiceImpl implements UserService {

    private UserMapper userMapper = MapperUtil.getSqlSession().getMapper(UserMapper.class);

    @Override
    public Boolean addUser(SysUser sysUser) {

        // TODO 根据用户名查询用户   注册失败  提示用户注册信息已存在

        Integer line = userMapper.insertUser(sysUser);
        return line > 0;
    }

    @Override
    public Map<String, Object> login(SysUser sysUser) {

        //根据用户名查询的结果
        SysUser user = userMapper.selectByUserName(sysUser.getUsername());

        HashMap<String, Object> map = new HashMap<>();
        //1.判定user是否为空  true  账号不存在 请注册账号  false -> 验证用户密码
        if (user == null) {
            map.put("code", 500);
            map.put("msg", "账号不存在,请注册!");
        } else {
            // 前端用户传递密码
            String p1 = sysUser.getPassword();
            // 数据中存储密码
            String p2 = user.getPassword();
            //2.判定密码   false  提示密码不对   true ->判定账号状态
            if (p1.equals(p2)) {
                //3.用户账号是否可用    1 可用   0  禁用
                if (user.getState() == 0) {

                    map.put("code", 500);
                    map.put("msg", "账号被禁用,请联系管理员!");

                } else {
                    map.put("code", 0);
                    map.put("msg", "登录成功");
                    map.put("user", user);
                }

            } else {
                map.put("code", 500);
                map.put("msg", "请输入正确的密码");
            }

        }
        return map;
    }

    @Override
    public LayUtil selectAll(PageDTO pageDTO) {

        //开启分页
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getLimit());

        List<SysUserVO> sysUserVOS = userMapper.selectAll(pageDTO.getUsername());

        //将查询的结果封装成 pageInfo 对象 获取更多的分页参数    total
        PageInfo<SysUserVO> sysUserVOPageInfo = new PageInfo<>(sysUserVOS);
        //获取数据的总条数
        long total = sysUserVOPageInfo.getTotal();

        LayUtil layUtil = new LayUtil();
        layUtil.setCode(0);
        layUtil.setMsg("查询成功");
        layUtil.setData(sysUserVOS);
        layUtil.setCount(total);

        return layUtil;
    }

    @Override
    public Map<String, Object> updateUser(SysUser sysUser) {
        HashMap<String, Object> map = new HashMap<>();

        Integer line = userMapper.updateUser(sysUser);

        if (line > 0) {

            map.put("code", 0);
            map.put("msg", "更新成功!");

        } else {

            map.put("code", 500);
            map.put("msg", "更新失败!");
        }


        return map;
    }

    @Override
    public Map<String, Object> addUserInfo(SysUser sysUser) {

        Map<String, Object> map = new HashMap<>();
        //根据用户信息查询 账号是否存在  存在 则直接提示用户 用户名不可用
        SysUser usr = userMapper.selectByUserName(sysUser.getUsername());

        if (ObjectUtil.isEmpty(usr)) {  // 用户名可用

            Integer line = userMapper.addUser(sysUser);

            if (line > 0) {
                map.put("code", 0);
                map.put("msg", "用户信息添加成功!");
            } else {

                map.put("code", 500);
                map.put("msg", "用户信息添加失败!");
            }

        } else {

            map.put("code", 500);
            map.put("msg", "用户名已被注册!");

        }
        return map;
    }

    @Override
    public Map<String, Object> deleteByIds(String ids) {

        Map<String, Object> map = new HashMap<String, Object>();

        Integer line = userMapper.deleteByIds(ids);

        if (line > 0) {

            map.put("code", 0);
            map.put("msg", "用户信息删除成功!");

        } else {
            map.put("code", 500);
            map.put("msg", "用户信息删除失败!");
        }
        return map;
    }

    @Override
    public Map<String, Object> updateState(Integer id, Integer state) {
        Map<String, Object> map = new HashMap<>();
        Integer line = userMapper.updateState(id, state);

        if (line > 0) {
            map.put("code", 0);
            map.put("msg", "状态更新成功");
        } else {
            map.put("code", 500);
            map.put("msg", "状态更新失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> replacepwd(Integer id, String pwd) {
        Map<String, Object> map = new HashMap<>();
        Integer line = userMapper.replacepwd(id, pwd);
        if (line > 0) {
            map.put("code", 0);
            map.put("msg", "密码修改成功");
        } else {
            map.put("code", 500);
            map.put("msg", "密码修改失败");
        }
        return map;
    }


}
